<?php
/* --------------------------------------------------------------
   IclearCore.php 2011-09-05 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/
?><?php
/*************************************************************************

  $Id: IclearCore.php 318 2009-08-19 10:53:17Z dis $

  iclear payment system - because secure is simply secure
  http://www.iclear.de

  Copyright (c) 2001 - 2009 iclear

  Released under the GNU General Public License

************************************************************************

                    All rights reserved.

  This program is free software licensed under the GNU General Public License (GPL).

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA

*************************************************************************/

  if(!defined('IC_SEC')) {
    die('No external calls allowed!');
  }

  require dirname(__FILE__) . '/IclearConfig.php';

  class IclearCore {
    var $systemID = '';
    var $dirCache = array();
    var $urlCache = array();
    var $objectCache = array();
    var $soapVarMap = false;
    
    var $proxy = false;
    
    var $lang = false;
    
    var $error = false;
    
    var $icVersion = '';
    
    function IclearCore($icLang = false) {
      $this->icVersion = '$Id: IclearCore.php 318 2009-08-19 10:53:17Z dis $';
      
      $this->detectSystem();
      if(!class_exists('IclearBase')) {
        require_once $this->getPath('iclear') . 'IclearBase.php';
      }
      
      if($icLang) {
        $this->lang =& $icLang;
      }
      
      $this->error = $this->getObject('IclearError');
      
    }
    
    function version() {
    	return $this->icVersion;
    }
    
    function debug($level = false) {
      return IC_DEBUG;
    }
    
    function systemID() {
      return $this->systemID;
    }
    
    function detectSystem() {
      $this->systemID = '';
      $root = $this->getPath();
      
      if(defined('IC_SYSTEM_TYPE') && IC_SYSTEM_TYPE) {
        $this->systemID = IC_SYSTEM_TYPE;
      } else {
	      if(is_dir($root.'includes/languages')) {
	        // looks like an osc version
	        if(file_exists($root.'includes/filenames.php')) {
	          // it's a ms2 version - or higher
	          $this->systemID = 'osc22ms2';
	        } else {
	          $this->systemID = 'osc22ms1';
	        }
	      } elseif(file_exists($root.'inc/xtc_add_tax.inc.php')) {
	        // looks like a xtc version
	        if(is_dir($root.'includes/classes/Smarty_2.6.10')) {
	        // XT:C 3.0.4 SP1
	          $this->systemID = 'xtc304sp1';
	        } elseif(is_dir($root.'includes/classes/Smarty')) {
	        // XT:C 3.0.4 SP2
	        $this->systemID = 'xtc304sp2';
	        }
	      } elseif(is_dir($root.'xtAdmin')) {
	      	// seems 2B veyton
	      	$this->systemID = 'veyton4';
	      }
      }
      
      return $this->systemID;
    }
    
    function getPath($location = 'root', $abs = true) {
      $pathout = '';
      if(!$location) {
        $location = 'root';
      }
      if(!isset($this->dirCache[$location])) {
  
        if( !isset($this->dirCache['root']) ) {
// TODO: remove static root dir assignment!!! Or allow 2 specifiy alternative location...
          $root = str_replace('\\', '/', dirname(__FILE__));
          
          $this->dirCache['docrootarr'] = explode('/', $root);
          //array_shift($this->dirCache['docrootarr']);
          $this->dirCache['docroot'] = $root;
          $this->dirCache['root'] = preg_replace('/iclear$/', "", $root);
          $this->dirCache['rootarr'] = explode('/', $this->dirCache['root']);
          
          unset($path);
        }
        
        if($location && $location != 'root') {
          $path[] = preg_replace('!/$!', '', $this->dirCache['root']); // remove trailing slash -> implode action below!
          switch($location) {
            case 'root':
              // TODO: actually nothing...
              break;
  
            case 'iclear':
              array_push($path, 'iclear');
              break;
  
            case 'lib':
              $path = array_merge($path, array('iclear', 'lib'));
              break;
  
            case 'wrapper':
              $path = array_merge($path, array('iclear', 'wrappers'));
              break;
  
            case 'config':
              $path = $this->dirCache['docrootarr'];
              array_pop($path);
              break;
  
          }
          $pathout = implode(IC_DS, $path) . IC_DS;
          $this->dirCache[$location] = $pathout;
        } else {
          $pathout = $this->dirCache['root'];
        }
      } else {
        $pathout = $this->dirCache[$location];
      }
  
      if(!$abs && $pathout) {
        $pathrel = preg_replace('!' . str_replace('\\', '/', $this->dirCache['root']) . '!', '', $this->dirCache[$location]);
        $pathout = IC_DS . implode(IC_DS, array($this->dirCache['rootarr'][sizeof($this->dirCache['rootarr']) - 2], $pathrel));
      }

	  // BOF GM_MOD:
	  $pathout = str_replace('\\', '/', $pathout);

      return $pathout;
    }
    
    
    function getBaseURL($location = '', $https = false) {
// IIS patch - HTTPS is always present, but contains of in case of none secure calls
      if((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') || $https==true) {
        $protocol="https";
      } else {
        $protocol="http";
      }
      
     	$requestURI = $_SERVER['PHP_SELF'];
      
      if( ($offset = strpos($requestURI, 'iclear')) === false ) {
        $offset = strrpos($requestURI, '/') + 1;
      }

      $path = substr($requestURI, 0,$offset);

      if(!defined('IC_URI_SHOP') || !IC_URI_SHOP) {
        if($this->proxy) {
          $server = $this->proxy->httpHost($https || isset($_SERVER['HTTPS']));
        } elseif(isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
        	$server = $_SERVER['HTTP_X_FORWARDED_HOST'];
        } else {
          $server = $_SERVER['SERVER_NAME'];
        }
        $url = $protocol . '://' . $server  . $path;
      } else {
        $url = IC_URI_SHOP;
      }
      $url = preg_replace('!//$!', '/', $url);

      return $url;
    }
    
    function getLanguage() {
      $rv = false;
      if($this->lang) {
        $rv =& $this->lang;
      } else {
        $rv =& $this->setLanguage();
      }
      return $rv;
    }
    
    function setLanguage($languageISO = 'DE') {
      if($languageISO) {
        $languageISO = strtolower($languageISO);
        if($this->lang) {
          $this->lang->setLanguage($languageISO);
        } else {
          $class = 'IclearLanguage';
          if(!class_exists($class)) {
            require_once $this->getPath('iclear') . $class.'.php';
          }
          $this->lang = new $class($languageISO);
        }
      }
      return $this->lang;
    }
    
    function &getProxy($instance = true) {
      return $this->getObject('IclearProxy', $instance);
    }
    
    function &getBasket($instance = true) {
      return $this->getObject('IclearBasket', $instance);
    }
    
    function setObject($class ='', &$obj) {
      $rc = false;
      if(class_exists($class) && is_a($obj, $class)) {
        $this->objectCache[$class] =& $obj;
        $rc = true;
      }
      return $rc;
    }
    
    function &getObject($class, $instance = false, $filename = '') {
      $rv = false;
      if($instance && isset($this->objectCache[$class])) {
        $rv =& $this->objectCache[$class];
      } else {
        if(!class_exists($class)) {
          if($filename) {
            $path = $filename;
          } else {
            $path = $this->getPath('iclear') . $class.'.php';
          }
          require_once $path;
        }
        $rv =& new $class($this);
        if(is_subclass_of($rv, 'IclearBase')) {
          $rv->icError =& $this->getObject('IclearError', true);
        }
        $this->objectCache[$class] =& $rv;
      }
      return $rv;
    }
    
    function &getSoapClient($instance = false) {
      
      $class = 'IclearSoapClient';
      if(! IC_INTERNAL_SOAP_CLIENT && class_exists('SoapClient')) {

        $classfile = $class.'.php5.php';
      } else {
        $classfile = $class.'.nusoap.php';
      }
      
      return $this->getObject($class, $instance, $classfile);
    }
    
    function &getSoapServer($instance = false) {
      
      $class = 'IclearSoapServer';
      if(class_exists('Soap__Server')) {
        $classfile = $class.'.php5.php';
      } else {
        $classfile = $class.'.nusoap.php';
      }
      
      return $this->getObject($class, $instance, $classfile);
    }

    function perform($object = false, $function = '', $params = false) {
      $rv = false;
      
      if(is_object($object) && $function) {
        if(!$params) {
          $params = array();
        }
        if( is_callable(array($object, $function)) ) {
          if(version_compare(PHP_VERSION, '5.0.0', '>')) {
            eval('
              try {
                $rv = call_user_func_array(array($object, $function), $params);
              } catch(Exception $e) {
                // Error case
              }');
        	} else {
            $rv = call_user_func_array(array($object, $function), $params);
        	}
        }
      }
      return $rv;
    }
    
  }
  
  global $icCore, $icLang;
  if(!is_object($icCore)) {
    $icCore = new IclearCore($icLang);
  }
  
?>